package org.example.word.utils;

import java.time.LocalDate;

public class DateUtils {

    public static String calculateShengxiao(LocalDate date) {
        int year = date.getYear();
        String[] zodiacSigns = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
        return zodiacSigns[(year - 1900) % 12];
    }

    public static String[] getBaZi(LocalDate birthDate, String timeRange) {
        // 这里仅作为示例，实际应用中需要复杂的天干地支转换逻辑
        // 将日期转换为干支年、月、日
        String yearGanZhi = getGanZhiByYear(birthDate.getYear());
        String monthGanZhi = getGanZhiByMonth(birthDate.getMonthValue());
        String dayGanZhi = getGanZhiByDay(birthDate.getDayOfMonth());

        // 将时间范围转换为对应的干支时
        String hourGanZhi = getGanZhiByHour(timeRange);

        String[] bazi = new String[4];
        bazi[0] = yearGanZhi;
        bazi[1] = monthGanZhi;
        bazi[2] = dayGanZhi;
        bazi[3] = hourGanZhi;
        return bazi;
    }

    private static String getGanZhiByYear(int year) {
        // 这里是简化的年干支计算
        // 实际应用中需要根据农历的干支纪年规则计算
        int baseYear = 1900; // 1900年是庚子年（假设为基准年）
        int yearDiff = year - baseYear;
        String[] tianGan = {"庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己"};
        String[] diZhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        int ganIndex = (yearDiff % 10 + 10) % 10;
        int zhiIndex = (yearDiff % 12 + 12) % 12;
        return tianGan[ganIndex] + diZhi[zhiIndex];
    }

    private static String getGanZhiByMonth(int month) {
        // 这里是简化的月干支计算
        // 实际应用中需要根据节气来判断月干支
        String[] tianGan = {"丙", "丁", "戊", "己", "庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁"};
        String[] diZhi = {"寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥", "子", "丑"};
        int index = (month - 1) % 12;
        return tianGan[index] + diZhi[index];
    }

    private static String getGanZhiByDay(int day) {
        // 这里是简化的日干支计算
        // 实际应用中需要根据农历的干支纪日规则计算
        String[] tianGan = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
        String[] diZhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        int ganIndex = (day - 1) % 10;
        int zhiIndex = (day - 1) % 12;
        return tianGan[ganIndex] + diZhi[zhiIndex];
    }

    private static String getGanZhiByHour(String timeRange) {
        // 这里是简化的时干支计算
        // 实际应用中需要根据时辰来判断时干支
        // 假设时间范围为 "HH:MM-HH:MM" 格式，提取开始小时
        String[] parts = timeRange.split("-");
        if (parts.length < 2) {
            return "未知时辰";
        }
        String startHourStr = parts[0].split(":")[0];
        int startHour = Integer.parseInt(startHourStr);

        String[] diZhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        int zhiIndex = startHour / 2;
        String[] tianGan = {"甲", "丙", "戊", "庚", "壬", "乙", "丁", "己", "辛", "癸", "甲", "丙"};
        int ganIndex = startHour / 2;
        return tianGan[ganIndex] + diZhi[zhiIndex];
    }

}
